//+------------------------------------------------------------------+
//|                                                          bbi.mq4 |
//|                                                    Dimitri Rudel |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Dimitri Rudel"
#property link      ""

#include <basic.mqh>
#include <tradeManager.mqh>


extern double exRsiLow = 30;
extern double exRsiHigh = 70;
extern double exRsiLong = 35;
extern double exRsiShort = 65;

extern double exAdx = 14;
extern double exAdxMax = 27;



extern int exRsi = 14;
extern int exBb = 50;
extern int exBbDeviation = 4;//50

extern int exCntCandleRsiIn = 1;
extern int exCntCandleRsiOut = 2;

extern int exEntrySlPip = 15;
extern int exTakeProfitPip = 14;

//trailStop
extern string TRAILSTOP = "TRAILSTOP";
extern int exTrailStop = 1;
extern int exCntCandlesAboveBbMid = 4;
extern int exPipTrailSl = 15;


double bbLower = 0;
double bbMid = 0;
double bbUpper = 0;
double adxMain = 0;
double rsi = 0;

string orderComment = "EA-BBI";

datetime oldTime = 0;
int oldBars = 0;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
      b_debug("BBI INIT");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
      b_debug("BBI DEINIT");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----

     if(oldTime == Time){
         return(0);   
      }
      oldTime = Time;
      
     if(Close[exBb] <= 0){
         b_debug("to less Bars: " + Bars);
         return(0);
     }
      
     
     //Print("time:",oldTime);
     
      bbLower = iBands(Symbol(),0,exBb,exBbDeviation,0,PRICE_CLOSE,MODE_LOWER,1); 
      bbUpper = iBands(Symbol(),0,exBb,exBbDeviation,0,PRICE_CLOSE,MODE_UPPER,1);  
      bbMid = (bbLower + bbUpper)/2;
      adxMain = iADX(Symbol(),0,exAdx,PRICE_CLOSE,MODE_MAIN,1);
      rsi = iRSI(Symbol(),0,exRsi,PRICE_CLOSE,1);
      
      tmgr_checkExit();
      
      if(exTrailStop == 1){
         trailStop();
      }
      if(!b_isTradeTime()){
         b_debug("no Tradetime !");
         return(0);
      }

      if(adxMain < exAdxMax ){
         b_debug("no Adx/AdxMax !"+ adxMain +"/"+exAdxMax);
         return (0);
      }
      
      
         
      if(rsi > exRsiShort){
         
         bool cntCandleClose = true;
         for(int i = 1; i <= exCntCandleRsiOut; i++){
            if(iRSI(Symbol(),0,exRsi,PRICE_CLOSE,i) < exRsiHigh){        
            }else{
               cntCandleClose = false;
               break;
            }
         }
            
         if(cntCandleClose){
               
            if(iRSI(Symbol(),0,exRsi,PRICE_CLOSE,exCntCandleRsiOut+1) > exRsiHigh){
               double tp = Bid - b_getPriceByPip(exTakeProfitPip);
               double sl = bbUpper + b_getPriceByPip(exEntrySlPip);
               b_OrderSend(Symbol(),1,0,Bid,3,sl,tp,orderComment);   
            }              
         }
         
      }else if(rsi > exRsiLong){
         
         cntCandleClose = true;
         for(i = 1; i <= exCntCandleRsiOut; i++){
            if(iRSI(Symbol(),0,exRsi,PRICE_CLOSE,i) > exRsiLow){        
            }else{
               cntCandleClose = false;
               break;
            }
         }   
         if(cntCandleClose){
               
            if(iRSI(Symbol(),0,exRsi,PRICE_CLOSE,exCntCandleRsiOut+1) < exRsiLow){
               tp = Ask + b_getPriceByPip(exTakeProfitPip);
               sl = bbLower - b_getPriceByPip(exEntrySlPip);
               b_OrderSend(Symbol(),0,0,Ask,3,sl,tp,orderComment);
            }   
         }       
      }else{
         b_debug("no rsi/exRsiShort/exRsiLong "+ rsi +"/"+exRsiShort+"/"+exRsiLong);
      }
      if(!cntCandleClose){
         b_debug("no cntCandleRsiOut "+exCntCandleRsiOut);
      }
   return(0);
  }
//+------------------------------------------------------------------+

void trailStop(){
   for(int i=0; i < OrdersTotal(); i++) {
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
         double stoploss = OrderStopLoss();
         if ( OrderSymbol()==Symbol()) {
           //Candles closes over the mid band
           
           bool trail = true;
           if(OrderType() == 0){
               for(int j = 1; j <= exCntCandlesAboveBbMid; j++){
                  if(Close[j] < bbMid){
                     trail = false;
                     break;
                  } 
               }
           }else{
               for(int k = 1; k <= exCntCandlesAboveBbMid; k++){
                  if(Close[k] > bbMid){
                     trail = false;
                     break;
                  } 
               }
           }
           if(trail){
               if(OrderType() == 0){
                  trail = stoploss < bbMid - b_getPriceByPip(exPipTrailSl);
                  stoploss = bbMid - b_getPriceByPip(exPipTrailSl);           
               }else{
                  trail = stoploss > bbMid + b_getPriceByPip(exPipTrailSl);
                  stoploss = bbMid + b_getPriceByPip(exPipTrailSl);
                  
           
               }
               if(trail){
                  b_OrderModify(OrderTicket(),OrderOpenPrice(),stoploss,OrderTakeProfit(),0,Blue);
               }
               
           }   
         }
      }
   }
}